<?php
if (!defined('ROOT')) exit('SYTbHelper');
//封禁相关
class del_model extends base {
	private $keyword;
	private $tieba;
	static private $_instance=NULL;
	static public function getInstance () {
		if (!self::$_instance) self::$_instance=new self;
		return self::$_instance;
	}
	public function __construct () {
		parent::init();
		$_ENV['_now']='del';
		$this->keyword=$this->db('get','delkeyword');
		$this->tieba=$this->db('get','deltieba');
	}
	//删除一个
	public function del_one ($fid,$tid,$tieba) {
		$post=array(
			'commit_fr'=>'pb',
			'ie'=>'utf-8',
			'tbs'=>$this->get_tbs(),
			'kw'=>$tieba,
			'fid'=>$fid,
			'tid'=>$tid
		);
		$re=jd(curl_get($this->mkurl('f/commit/thread/delete'),array('post'=>$post)));
		if (!$re) {
			echo '<li class="list-group-item">JSON解析错误</li>';
			return FALSE;
		} elseif ($re['no']==0) {
			echo '<li class="list-group-item list-group-item-success">删帖成功！贴吧：',$tieba,'，贴子ID：',$tid;
			return TRUE;
		} else {
			echo '<li class="list-group-item list-group-item-warning">删帖失败！贴吧：',$tieba,'，贴子ID：',$tid;
			return FALSE;
		}
	}
	//获取单个贴吧
	public function del_tb ($tieba) {
		$tieba=$this->get_tbinfo($tieba);
		foreach ($tieba['thread_list'] as $thread) {
			if ($this->del_checkone($thread)) {
				if (defined('CRON')) { //Cron形式，使用TaskQueue
					task_add('del',array('option'=>'tie','tid'=>$thread['id'],'tieba'=>$tieba));
				} else {
					echo 'deltie.push("',$thread['id'],'");';
				}
			}
		}
	}
	//检查是否该删掉
	public function del_checkone ($thread) {
		$keyword=$this->del_keywords();
			$title=strtolower($thread['title']);
			$text=strtolower($thread['abstract'][0]['text']);
		if ($this->getval('del_isreg')) { //正则方式
			foreach ($keyword as $one) {
				if (preg_match('/'.$one.'/',$title) || preg_match('/'.$one.'/',$text)) return TRUE;
			}
			return FALSE;
		} else { //非正则
			return ($this->sstrpos($text) || $this->sstrpos($title));
		}
	}
	private function sstrpos ($keys,$text) {
	foreach ((array)$keys as $one) {
	if (strpos($one,$text)!==FALSE) return TRUE;
	}
	return FALSE;
	}
	//获取关键字
	public function del_keywords () {
		return $this->keyword;
	}
	//获取贴吧
	public function del_tieba () {
		return $this->tieba;
	}
	//修改关键字
	public function del_setKeyword ($keyword) {
		$this->keyword=$keyword;
		$this->db('set','delkeyword',$keyword);
	}
	//修改贴吧
	public function del_setTieba ($tieba) {
		$this->tieba=$tieba;
		$this->db('set','deltieba',$tieba);
	}
	//CRON
	public function cron () {
		//检查是否已有option
		$option=getgpc('option');
		$tieba=urldecode(getgpc('tieba'));
		$tid=getgpc('tid');
		if (!empty($option)) {
			if ($option==='tie') { //删帖
				$this->del_one($this->get_fid($tieba),$tid,$tieba);
			} elseif ($option==='tieba') { //检查单个贴吧
				$this->del_tb($tieba);
			}
		} else { //获取贴吧并添加TaskQueue
			$tieba=$this->del_tieba();
			foreach ($tieba as $one) {
				task_add('del',array('option'=>'tieba','tieba'=>$tieba));
			}
		}
	}
	//关键词
	public function onkeyword () {
		if (isset($_POST['submit'])) {
			$keyword=getgpc('keyword','P');
			$keywords=array();
			foreach ($keyword as $k) {
				$k=trim($k);
				if (!empty($k)) $keywords[]=$k;
			}
			$this->del_setKeyword($keywords);
			$this->ShowTips('修改成功','success');
		} else {
			$keyword=$this->del_keywords();
			include(TPL.'del_keyword.html');
		}
	}
	//贴吧
	public function ontieba () {
		if (isset($_POST['submit'])) {
			$tieba=getgpc('tieba','P');
			$tiebas=array();
			foreach ($tieba as $k) {
				$k=trim($k);
				if (!empty($k)) $tiebas[]=$k;
			}
			$this->del_setTieba($tieba);
			$this->ShowTips('修改成功','success');
		} else {
			$tieba=$this->del_tieba();
			include(TPL.'del_tieba.html');
		}
	}
	//测试
	public function ontest () {
		$tid=getgpc('tid','P');
		$tieba=getgpc('tieba','P');
		if (!empty($tid)) {
			$this->del_one($this->get_fid($tieba),$tid,$tieba);
		} elseif (!empty($tieba)) {
			$this->del_tb($tieba);
		} else {
			include(TPL.'del_test.html');
		}
	}
}

?>